// Bertels Nick c1019387
package views;


import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import core.models.dataModels.BemanningAanduidenModel;
import core.models.domainModel.Personeel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JList;

public class BemanningAanduidenJdialog extends JDialog
{
	private static final long serialVersionUID = 1L;
	private int returnStatus = 0;

	public int getReturnStatus() {
		return returnStatus; // 0 = cancel, 1 = OK
	}
	private BemanningAanduidenModel _model;
	public BemanningAanduidenJdialog(BemanningAanduidenModel model) {
		
		
		
		
		_model = model;
		
		final JList list = new JList();
		
		final JComboBox cmbPiloten = new JComboBox();
		
		JLabel lblPiloten = new JLabel("Piloten :");
		
		JLabel lblStewardsStewardessen = new JLabel("Stewards / Stewardessen :");
		
		final JComboBox cmbStewardsStewardessen = new JComboBox();
		
		final DefaultListModel<String> listModel = new DefaultListModel<String>();
		
		JButton btnPilootToevoegen = new JButton("Toevoegen");
		btnPilootToevoegen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				
				Personeel p = (Personeel)cmbPiloten.getSelectedItem();
				cmbPiloten.removeItem((Personeel)p);
				listModel.addElement(p.getPersoon().getVoornaam() + " " + p.getPersoon().getNaam() + " ( " + p.get_personeelType() + ")");
				list.setModel(listModel);
				
				
				_model.add_personeel((Personeel)cmbPiloten.getSelectedItem());
				//BemanningAanduidenController.handleView(_model);
				
			}
		});
		
		JButton btnStuwardStewardessToevoegen = new JButton("Toevoegen");
		btnStuwardStewardessToevoegen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Personeel p = (Personeel)cmbStewardsStewardessen.getSelectedItem();
				cmbStewardsStewardessen.removeItem((Personeel)p);
				listModel.addElement(p.getPersoon().getVoornaam() + " " + p.getPersoon().getNaam() + " ( " + p.get_personeelType() + ")");
				list.setModel(listModel);
				
				
				_model.add_personeel((Personeel)cmbStewardsStewardessen.getSelectedItem());
				
				//BemanningAanduidenController.handleView(_model);
			}
		});
		
		JButton btnPersoneelToevoegen = new JButton("PersoneelToevoegen");
		btnPersoneelToevoegen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				int aantalPiloten = 0 , aantalStuwards = 0;
				if (_model.get_geselecteerd() != null)
				{
					for (Personeel p : _model.get_geselecteerd())
						if (p.get_personeelType().toString() == "Piloot")
							aantalPiloten++;
						else
							aantalStuwards++;
					
					if (aantalPiloten >= _model.getAantalPiloten() && aantalStuwards >= _model.getAantalStuwards())
					{
					returnStatus = 1;
					dispose();
					}
					else
					{
						JOptionPane.showMessageDialog(null, "Niet genoeg personeel toegevoegd.", "Fout", JOptionPane.ERROR_MESSAGE);
					}
				}
				else
					JOptionPane.showMessageDialog(null, "Geen personeel toegevoegd.", "Fout", JOptionPane.ERROR_MESSAGE);
				
					

			}
		});
		
		JLabel lblAantalPersoneelNodig = new JLabel("Aantal personeel nodig");
		
		lblAantalPersoneelNodig.setText("Er zijn " + _model.getAantalPiloten() + " piloten en " + _model.getAantalStuwards() + " stuwards nodig");
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(46)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblPiloten)
						.addComponent(lblStewardsStewardessen))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblAantalPersoneelNodig)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(cmbPiloten, GroupLayout.PREFERRED_SIZE, 147, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnPilootToevoegen))
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(list, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(btnPersoneelToevoegen, Alignment.LEADING)
								.addComponent(cmbStewardsStewardessen, Alignment.LEADING, 0, 147, Short.MAX_VALUE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnStuwardStewardessToevoegen, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(8, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(22)
					.addComponent(lblAantalPersoneelNodig)
					.addGap(35)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(cmbPiloten, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblPiloten)
						.addComponent(btnPilootToevoegen))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(cmbStewardsStewardessen, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblStewardsStewardessen)
						.addComponent(btnStuwardStewardessToevoegen))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(list, GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE)
					.addGap(25)
					.addComponent(btnPersoneelToevoegen)
					.addGap(52))
		);
		getContentPane().setLayout(groupLayout);
		
		
		for(Personeel p : model.get_mogelijkePiloten())
		{
			cmbPiloten.addItem(p);
		}
			
		for(Personeel p : model.get_mogelijkeStewardsStewardessen())
		{
			cmbStewardsStewardessen.addItem(p);
		}

	
	}
}
